﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Polls;

namespace NosoiFigthersWeb.PollDesigner {
	public partial class ElementEdit : System.Web.UI.Page {
		protected void Page_Load(object sender, EventArgs e) {
			if (!IsPostBack) {
				ViewState["answersList"] = null;
				answers.Items.Clear();

				minAnswersCount.Items.Clear();
				minAnswersCount.Items.Add(new ListItem() {
					Text = "0",
					Selected = true
				});
				maxAnswersCount.Items.Clear();
				maxAnswersCount.Items.Add(new ListItem() {
					Text = "0",
					Selected = true
				});

				fckeditor.Value = " ";
				obligatory.Checked = false;
				arrangements.Items[0].Selected = true;
				arrangements.Items[1].Selected = false;
				minValue.Text = "0";
				maxValue.Text = "0";

				TextButton_Click(null, null);
				if (Session["editedElement"] != null) {
					ViewState["answersList"] = new List<string>();
					fckeditor.Value = (Session["editedElement"] as Text).Content;

					if (Session["editedElement"] is MultipleChoiceQuestion) {
						MultipleChoiceQuestion mcq = Session["editedElement"] as MultipleChoiceQuestion;
						foreach (string str in mcq.Answers)
							(ViewState["answersList"] as List<string>).Add(str);
						if (mcq.AnswersArrangement == Arrangement.Vertical) {
							arrangements.Items[0].Selected = false;
							arrangements.Items[1].Selected = true;
						}
						for (int i = 1; i <= mcq.Answers.Count; ++i) {
							minAnswersCount.Items.Add(new ListItem() {
								Text = i.ToString()
							});
							maxAnswersCount.Items.Add(new ListItem() {
								Text = i.ToString()
							});
						}
						minAnswersCount.Items[0].Selected = false;
						maxAnswersCount.Items[0].Selected = false;
						minAnswersCount.Items[mcq.MinAnswers].Selected = true;
						maxAnswersCount.Items[mcq.MaxAnswers].Selected = true;

						MultiChoiceButton_Click(null, null);
					}
					else if (Session["editedElement"] is SingleChoiceQuestion) {
						SingleChoiceQuestion scq = Session["editedElement"] as SingleChoiceQuestion;
						if (scq.Obligatory == true)
							obligatory.Checked = true;
						foreach (string str in scq.Answers)
							(ViewState["answersList"] as List<string>).Add(str);
						if (scq.AnswersArrangement == Arrangement.Vertical) {
							arrangements.Items[0].Selected = false;
							arrangements.Items[1].Selected = true;
						}

						SingleChoiceButton_Click(null, null);
					}
					else if (Session["editedElement"] is NumberQuestion) {
						NumberQuestion nq = Session["editedElement"] as NumberQuestion;
						if (nq.Obligatory == true)
							obligatory.Checked = true;
						minValue.Text = nq.MinNumber.ToString();
						maxValue.Text = nq.MaxNumber.ToString();

						NumberButton_Click(null, null);
					}
					else if (Session["editedElement"] is OpenQuestion) {
						OpenQuestion oq = Session["editedElement"] as OpenQuestion;
						if (oq.Obligatory == true)
							obligatory.Checked = true;

						OpenButton_Click(null, null);
					}
				}

				BindAnswersList();
			}
		}
		private void HideAll() {
			answersPanel.Visible = false;
			answersPropertiesPanel.Visible = false;
			obligatory.Visible = false;
			answersCountExtremes.Visible = false;
			arrangements.Visible = false;
			valuesPanel.Visible = false;
		}
		protected void TextButton_Click(object sender, ImageClickEventArgs e) {
			HideAll();

			ViewState["questionType"] = "Text";
		}
		protected void SingleChoiceButton_Click(object sender, ImageClickEventArgs e) {
			HideAll();

			answersPanel.Visible = true;
			answersPropertiesPanel.Visible = true;
			obligatory.Visible = true;
			arrangements.Visible = true;

			ViewState["questionType"] = "SingleChoice";
		}
		protected void MultiChoiceButton_Click(object sender, ImageClickEventArgs e) {
			HideAll();

			answersPanel.Visible = true;
			answersPropertiesPanel.Visible = true;
			answersCountExtremes.Visible = true;
			arrangements.Visible = true;

			ViewState["questionType"] = "MultiChoice";
		}
		protected void OpenButton_Click(object sender, ImageClickEventArgs e) {
			HideAll();

			answersPropertiesPanel.Visible = true;
			obligatory.Visible = true;

			ViewState["questionType"] = "Open";
		}
		protected void NumberButton_Click(object sender, ImageClickEventArgs e) {
			HideAll();

			answersPropertiesPanel.Visible = true;
			obligatory.Visible = true;
			valuesPanel.Visible = true;

			ViewState["questionType"] = "Number";
		}
		protected void Answers_ItemDataBound(object sender, ListViewItemEventArgs e) {
			if (e.Item is ListViewDataItem) {
				TextBox tb = e.Item.FindControl("answer") as TextBox;
				tb.Text = (e.Item as ListViewDataItem).DataItem.ToString();
			}
		}
		private List<string> GetAnswersList() {
			List<string> answersList = null;
			if (ViewState["answersList"] != null)
				answersList = ViewState["answersList"] as List<string>;
			else
				ViewState["answersList"] = new List<string>();
			return answersList;
		}
		private void UpdateAnswersList() {
			List<string> answersList = new List<string>();
			foreach (ListViewItem item in answers.Items)
				if (item is ListViewDataItem) {
					TextBox tb = item.FindControl("answer") as TextBox;
					answersList.Add(tb.Text);
				}
			ViewState["answersList"] = answersList;
		}
		private void IncrementAnswersList() {
			List<string> answersList = GetAnswersList();
			answersList.Add("");
			ViewState["answersList"] = answersList;
		}
		private void BindAnswersList() {
			List<string> answersList = GetAnswersList();

			answers.DataSource = answersList;
			answers.DataBind();
		}
		protected void AddNewAnswer_Click(object sender, EventArgs e) {
			UpdateAnswersList();
			IncrementAnswersList();
			BindAnswersList();

			minAnswersCount.Items.Add(new ListItem() {
				Text = minAnswersCount.Items.Count.ToString()
			});
			maxAnswersCount.Items.Add(new ListItem() {
				Text = maxAnswersCount.Items.Count.ToString()
			});
		}
		protected void Down_Click(object sender, ImageClickEventArgs e) {
			int myIndex = ((sender as ImageButton).Parent as ListViewDataItem).DataItemIndex;
			int nextIndex = (myIndex + 1) % answers.Items.Count;

			ListViewDataItem tmp2 = answers.Items[myIndex];
			answers.Items[myIndex] = answers.Items[nextIndex];
			answers.Items[nextIndex] = tmp2;

			UpdateAnswersList();
			BindAnswersList();
		}
		protected void Up_Click(object sender, ImageClickEventArgs e) {
			int myIndex = ((sender as ImageButton).Parent as ListViewDataItem).DataItemIndex;
			int nextIndex = myIndex - 1;
			if (nextIndex < 0)
				nextIndex += answers.Items.Count;

			ListViewDataItem tmp2 = answers.Items[myIndex];
			answers.Items[myIndex] = answers.Items[nextIndex];
			answers.Items[nextIndex] = tmp2;

			UpdateAnswersList();
			BindAnswersList();
		}
		protected void DeleteAnswer_Click(object sender, ImageClickEventArgs e) {
			answers.Items.Remove((sender as ImageButton).Parent as ListViewDataItem);

			UpdateAnswersList();
			BindAnswersList();

			minAnswersCount.Items.RemoveAt(minAnswersCount.Items.Count - 1);
			maxAnswersCount.Items.RemoveAt(maxAnswersCount.Items.Count - 1);
		}
		protected void Save_Click(object sender, EventArgs e) {
			string questionType = ViewState["questionType"] as string;

			switch (questionType) {
				case "Text":
					Text text = new Text() {
						Content = fckeditor.Value
					};
					if (Session["editedElement"] != null)
						(Session["poll"] as Poll).Elements[(int)Session["editedElementIndex"]] =
							text;
					else {
						(Session["poll"] as Poll).AddElement(text);
						Session["editedElement"] = text;
						Session["editedElementIndex"] = (Session["poll"] as Poll).Elements.Count - 1;
					}
					break;
				case "SingleChoice":
					SingleChoiceQuestion scq = new SingleChoiceQuestion() {
						Content = fckeditor.Value,
						Obligatory = obligatory.Checked,
						AnswersArrangement = arrangements.SelectedIndex == 0 ?
								Arrangement.Horizontal : Arrangement.Vertical
					};
					UpdateAnswersList();
					foreach (string str in GetAnswersList())
						scq.AddAnswer(str);
					if (Session["editedElement"] != null)
						(Session["poll"] as Poll).Elements[(int)Session["editedElementIndex"]] =
							scq;
					else {
						(Session["poll"] as Poll).AddElement(scq);
						Session["editedElement"] = scq;
						Session["editedElementIndex"] = (Session["poll"] as Poll).Elements.Count - 1;
					}
					break;
				case "MultiChoice":
					MultipleChoiceQuestion mcq = new MultipleChoiceQuestion() {
						Content = fckeditor.Value,
						Obligatory = obligatory.Checked,
						MinAnswers = int.Parse(minAnswersCount.SelectedValue),
						MaxAnswers = int.Parse(maxAnswersCount.SelectedValue),
						AnswersArrangement = arrangements.SelectedIndex == 0 ?
								Arrangement.Horizontal : Arrangement.Vertical
					};
					UpdateAnswersList();
					foreach (string str in GetAnswersList())
						mcq.AddAnswer(str);
					if (Session["editedElement"] != null)
						(Session["poll"] as Poll).Elements[(int)Session["editedElementIndex"]] =
							mcq;
					else {
						(Session["poll"] as Poll).AddElement(mcq);
						Session["editedElement"] = mcq;
						Session["editedElementIndex"] = (Session["poll"] as Poll).Elements.Count - 1;
					}
					break;
				case "Open":
					OpenQuestion oq = new OpenQuestion() {
						Content = fckeditor.Value,
						Obligatory = obligatory.Checked
					};
					if (Session["editedElement"] != null)
						(Session["poll"] as Poll).Elements[(int)Session["editedElementIndex"]] =
							oq;
					else {
						(Session["poll"] as Poll).AddElement(oq);
						Session["editedElement"] = oq;
						Session["editedElementIndex"] = (Session["poll"] as Poll).Elements.Count - 1;
					}
					break;
				case "Number":
					NumberQuestion nq = new NumberQuestion() {
						Content = fckeditor.Value,
						Obligatory = obligatory.Checked,
						MinNumber = int.Parse(minValue.Text),
						MaxNumber = int.Parse(maxValue.Text)
					};
					if (Session["editedElement"] != null)
						(Session["poll"] as Poll).Elements[(int)Session["editedElementIndex"]] =
							nq;
					else {
						(Session["poll"] as Poll).AddElement(nq);
						Session["editedElement"] = nq;
						Session["editedElementIndex"] = (Session["poll"] as Poll).Elements.Count - 1;
					}
					break;
				default:
					break;
			}
		}
	}
}